<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2012 Grzegorz Banka <grzegorz@grzegorzbanka.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */

require_once(PATH_tslib.'class.tslib_pibase.php');


/**
 * Plugin 'MT dotPay' for the 'mt_paymenager' extension.
 *
 * @author	Grzegorz Banka <grzegorz@grzegorzbanka.com>
 * @package	TYPO3
 * @subpackage	tx_mtpaymenager
 */
class tx_mtpaymenager_pi2 extends tslib_pibase {
	var $prefixId      = 'tx_mtpaymenager_pi2';		// Same as class name
	var $scriptRelPath = 'pi2/class.tx_mtpaymenager_pi2.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'mt_paymenager';	// The extension key.
	var $pi_checkCHash = true;
	
	/**
	 * The main method of the PlugIn
	 *
	 * @param	string		$content: The PlugIn content
	 * @param	array		$conf: The PlugIn configuration
	 * @return	The content that is displayed on the website
	 */
	function main($content, $conf) {
		$this->conf = $conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
		
	
		$content='
			<strong>This is a few paragraphs:</strong><br />
			<p>This is line 1</p>
			<p>This is line 2</p>
	
			<h3>This is a form:</h3>
			<form action="'.$this->pi_getPageLink($GLOBALS['TSFE']->id).'" method="POST">
				<input type="text" name="'.$this->prefixId.'[input_field]" value="'.htmlspecialchars($this->piVars['input_field']).'">
				<input type="submit" name="'.$this->prefixId.'[submit_button]" value="'.htmlspecialchars($this->pi_getLL('submit_button_label')).'">
			</form>
			<br />
			<p>You can click here to '.$this->pi_linkToPage('get to this page again',$GLOBALS['TSFE']->id).'</p>
		';
	
		return $this->pi_wrapInBaseClass($content);
	}
	function get_security_code($pid,$code)
	{
		$where = 'deleted=0 AND hidden=0 AND pid='.$pid.' AND code="'.$code.'"';
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('code','tx_mtpaymenager_code',$where);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			return true;
		}	
		return false;
	}
	function use_payment_code($pid,$code)
	{
		$query = $GLOBALS['TYPO3_DB']->UPDATEquery('tx_mtpaymenager_code','pid='.$pid.' AND code="'.$code.'"',array('hidden'=>1));		
		$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);
	}
	function get_payment_info($pid,$what,$templateFile='',$conf)
	{
		//print_r($conf);
		//$this->conf=$conf;
		if($templateFile=='') $templateFile=$this->conf['templateFile'];
		$this->cObj = t3lib_div::makeInstance('tslib_cObj');
			
		$this->templateCode =  $this->cObj->fileResource($templateFile);		
		$template['info'] = $this->cObj->getSubpart($this->templateCode,'###INFO###');
		$template['sms'] = $this->cObj->getSubpart($this->templateCode,'###SMS###');
		$template['transfer'] = $this->cObj->getSubpart($this->templateCode,'###TRANSFER###');
		$template['multi_code'] = $this->cObj->getSubpart($this->templateCode,'###MULTI_CODE###');
		
		$where = 'deleted=0 AND hidden=0 AND pid='.$pid;
		
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('title,price,idseller','tx_mtpaymenager_setting',$where,'','',1);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			//print_r(str_replace(',','_',$row['price']));
			$sms_price=$this->get_sms_price($conf['sms.']['number.'],$row['price']);
			if($sms_price!='' && $conf['sms.']['number.'][str_replace('.','_',$sms_price)]!='')
			{	
				$markerArray['###SMS_NAME###']=$row['title'];
				$markerArray['###SMS_NUMBER###']=$conf['sms.']['number.'][str_replace('.','_',$sms_price)];
				$markerArray['###SMS_PRICE###']=number_format($sms_price*$conf['tax_vat'],2,'.','');
				$subpartArray['###SMS###']=$this->cObj->substituteMarkerArrayCached($template['sms'],$markerArray);
			}else $subpartArray['###SMS###']='';
			
			$markerArray['###TRANSFER_PRICE###']=$row['price'];
			$markerArray['###DOTPAY_PAY###']='https://ssl.dotpay.pl/?id='.$row['idseller'].'&code='.$row['title'];
			$markerArray['###WEB_REGULAMIN###']=$conf['code_regulamin'];
			
			if($conf['multi_payment_title']!='')
			{
				$markerArray['###MULTI_PAYMENT###']='https://ssl.dotpay.pl/?id='.$row['idseller'].'&code='.$conf['multi_payment_title'];
				$markerArray['###MULTI_COUNT###']=$conf['multi_count'];
				$markerArray['###MULTI_LINK###']=$conf['multi_link']; 
				if($conf['show_multi']==1) $subpartArray['###MULTI_CODE###']=$this->cObj->substituteMarkerArrayCached($template['multi_code'],$markerArray);
				else $subpartArray['###MULTI_CODE###']='';
			}else $subpartArray['###MULTI_CODE###']='';
		}
		return $this->cObj->substituteMarkerArrayCached($template['info'],$markerArray,$subpartArray);
	}
	function get_sms_price($all,$price)
	{
		$key=array_keys($all);
		for($i=0; $i<count($key); $i++)
		{
			$newkey=str_replace('_','.',$key[$i]);
			if($newkey>$price) 
			{
				return $newkey;
				break;
			}	
		}
	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_paymenager/pi2/class.tx_mtpaymenager_pi2.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_paymenager/pi2/class.tx_mtpaymenager_pi2.php']);
}

?>